#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'teffy'

'''
@filename forms
@@created on 2018-01-15
@poject:python_flask_demo
@auther:teffy
'''

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo

from ..models import User


class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 64), Email()])

    password = PasswordField('Password', validators=[DataRequired()])
    remember_me = BooleanField('Keep me logged in')
    submit = SubmitField('Log In')


class RegisterationForm(FlaskForm):
    email = StringField("Email", validators=[
        DataRequired(),
        Length(1, 64),
        Email()])
    username = StringField('Username', validators=[
        DataRequired(), Length(1, 64),
        Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
               'Usernames must have only letters numbers, dots or underscores')])
    password = PasswordField('Password', validators=[
        DataRequired(), EqualTo('password2', message='Passwords must match.')
    ])
    password2 = PasswordField('Confirm password', validators=[
        DataRequired()
    ])
    submit = SubmitField('Register')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('Email alread registered.')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('Username alread in user.')

class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('Old Password',validators=[DataRequired()])
    password =  PasswordField('New Password',validators=[DataRequired(),EqualTo('password2',message='New Passwords must match.')])
    password2 =  PasswordField('Confirm new Password',validators=[DataRequired()])
    submit = SubmitField('Update Password')

class ResetPasswordRequestForm(FlaskForm):
    email = StringField("Email", validators=[
        DataRequired(),
        Length(1, 64),
        Email()])
    submit = SubmitField('Reset Password')

class ResetPasswordForm(FlaskForm):
    password =  PasswordField('New Password',validators=[DataRequired(),EqualTo('password2',message='New Passwords must match.')])
    password2 =  PasswordField('Confirm new Password',validators=[DataRequired()])
    submit = SubmitField('Reset Password')

class ChangeEmailFrom(FlaskForm):
    email = StringField('New Email',validators=[DataRequired(),Length(1,64),Email()])
    password = PasswordField('Password',validators=[DataRequired()])

    submit = SubmitField('Update Email Address')

    def validate_email(self,field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('Email already registered.')